# debugtalk.py
import os
import time
import hashlib
import requests
import json
import MySQLdb
import base64
"""
闪电借款
"""
def clearCreditInfo():
    """
    清风控2.0用户信息
    bizType：业务线
    用户：袁维鑫
    """
    idCardNo = '230221199009294217'
    flag = False
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/clearUserInfo' + '|POST||' + '{"idcard":"%s","vendor":"weshare","bizType":"sdjk"}' % idCardNo + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    url = 'https://sight1.zz.com/sight/mock/api/clearUserInfo'
    headers = {'Content-type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': idCardNo, 'vendor': 'weshare', 'bizType': 'sdjk'}
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False).json()
    if r['respCode'] == "0000":
        flag = True
        print('清除风控成功')
    return flag


def test_User():
    """
    清除借款用户信息
    用户：袁维鑫
    """
    userGid = "175cec30-c64b-440f-beaa-31fd1f3f2e7d"

    expire_time = int(time.time()) + 186400
    sign = '|v2-lKyUUfUFmvBD8zMdvPOs-%s|2Ntq2qRVfSsGPfM1HQZZcnuwKIyb9fuUTizdXEzA|/harbor/eapi/v1/autotest/init|POST|b=0|{"userGid": "%s"}|' % (
        expire_time, userGid)
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ("v2", "lKyUUfUFmvBD8zMdvPOs", expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json;charset=utf-8', 'X-WeshareAuth-Token': token}
    data = {"userGid": userGid}
    url = 'https://harbor.weshare.com.cn/harbor/eapi/v1/autotest/init?b=0'

    # 发送请求
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    assert result['status'] == 0, '重置用户数据失败'
    if clearCreditInfo():
        print('重置用户数据成功')
    else:
        print('重置用户数据失败')
        assert 1 == 0, '重置风控数据失败'

        
# def enterText(text):
#     """
#     解决实名时储蓄卡号乱序问题
#     """
#     time.sleep(5)
#     for i in range(len(text)):
#         os.system('adb shell input text ' + text[i])
        
def enterText(text, driver):
    """kwycode输入方法
    解决实名绑卡时，输入银行卡号乱序问题
    param:
        text:输入内容
        driver:服务实体，忽略
    """
    key = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
           'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
           'w', 'x', 'y', 'z']
    keyCode = ['7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '29', '30', '31',
               '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44',
               '45', '46', '47', '48', '49', '50', '51', '52', '53', '54']

    for s in str(text):
        for k in range(len(key)):
            if s == key[k]:
                driver.press_keycode(keyCode[k])
                time.sleep(1)
            else:
                pass


def phone():
    """
    手机认证
    """
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"1","vendor":"weshare","bizType":"sdjk"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())

    headers = {'Content-Type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': '230221199009294217', 'authType': '1', 'vendor': 'weshare', 'bizType': 'sdjk'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('手机认证成功')
        return flag
    else:
        flag = False
        print('手机认证失败')
        return flag


def XY():
    """
    信用卡认证
    """
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"12","vendor":"weshare","bizType":"sdjk"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': '230221199009294217', 'authType': '12', 'vendor': 'weshare', 'bizType': 'sdjk'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('信用卡认证成功')
        return flag
    else:
        flag = False
        print('信用卡认证失败')
        return flag


def SF():
    """
    身份认证
    """
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"17","vendor":"weshare","bizType":"sdjk"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': '230221199009294217', 'authType': '17', 'vendor': 'weshare', 'bizType': 'sdjk'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('身份认证成功')
        return flag
    else:
        flag = False
        print('身份认证失败')
        return flag
        
def baseinfo():
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"45","vendor":"weshare","bizType":"sdjk"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json','X-WeshareAuth-Token': token}
    data = {'idcard':'230221199009294217','authType':'45','vendor':'weshare','bizType':'sdjk'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r= requests.post(url=url,data=json.dumps(data), headers=headers,verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('基本信息认证成功')
        return flag
    else:
        flag = False
        print('基本信息认证失败')
        return flag

def contact():
    """
    紧急联系人
    """
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"13","vendor":"weshare","bizType":"sdjk"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': '230221199009294217', 'authType': '13', 'vendor': 'weshare', 'bizType': 'sdjk'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('紧急联系人认证成功')
        return flag
    else:
        flag = False
        print('紧急联系人认证失败')
        return flag
        
# 查询数据库是否有借款数据
def db_loan():
    hosts = "db01.zz.com"
    ports = 4601
    users = "chenweinan"
    passwds = "nm3T47nkJZMTA7ar"
    dbs = "harbor"
    charsets = "utf8"
    t = int(time.time())
    sql = ('select count(*) from record_loan where create_time > (%d - 900) and loan_status = 6;') % t

    conn = MySQLdb.connect(host=hosts, port=ports, user=users, passwd=passwds, db=dbs, charset=charsets)
    cur = conn.cursor()
    cur.execute(sql)
    result = cur.fetchall()
    if result[0][0] == 0:
        raise Exception('借款失败')
    else:
        print(result[0][0])

#查询数据库是否有还款数据
def db_repayment():
    hosts = "db01.zz.com"
    ports = 4601
    users = "chenweinan"
    passwds = "nm3T47nkJZMTA7ar"
    dbs = "harbor"
    charsets = "utf8"
    t = int(time.time())
    sql = ('select count(*) from record_repayment where end_time > (%d - 900) and repayment_status = 1') % t

    conn = MySQLdb.connect(host=hosts, port=ports, user=users, passwd=passwds, db=dbs, charset=charsets)
    cur = conn.cursor()
    cur.execute(sql)
    result = cur.fetchall()
    if result[0][0] == 0:
        raise Exception('还款失败')
    else:
        print (result[0][0])

"""
掌e贷
"""
def clearCreditInfo_zhed():
    """
    清风控2.0用户信息
    bizType：业务线
    用户：袁维鑫
    """
    idCardNo = '230221199009294217'
    flag = False
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/clearUserInfo' + '|POST||' + '{"idcard":"%s","vendor":"weshare","bizType":"zhed"}' % idCardNo + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    url = 'https://sight1.zz.com/sight/mock/api/clearUserInfo'
    headers = {'Content-type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': idCardNo, 'vendor': 'weshare', 'bizType': 'zhed'}
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False).json()
    if r['respCode'] == "0000":
        flag = True
        print('清除风控成功')
    return flag


def test_User_zhed():
    """
    清除借款用户信息
    用户：袁维鑫
    """
    userGid = "35f4501c-7719-4547-b51e-3da23432c5b8"

    expire_time = int(time.time()) + 186400
    sign = '|v2-lKyUUfUFmvBD8zMdvPOs-%s|2Ntq2qRVfSsGPfM1HQZZcnuwKIyb9fuUTizdXEzA|/harbor/eapi/v1/autotest/init|POST|b=0|{"userGid": "%s"}|' % (
        expire_time, userGid)
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ("v2", "lKyUUfUFmvBD8zMdvPOs", expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json;charset=utf-8', 'X-WeshareAuth-Token': token}
    data = {"userGid": userGid}
    url = 'https://harbor.weshare.com.cn/harbor/eapi/v1/autotest/init?b=7'

    # 发送请求
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    assert result['status'] == 0, '重置用户数据失败'
    if clearCreditInfo_zhed():
        print('重置用户数据成功')
    else:
        print('重置用户数据失败')
        assert 1 == 0, '重置风控数据失败'
        
# def enterText(text):
#     """
#     解决实名时储蓄卡号乱序问题
#     """
#     time.sleep(5)
#     for i in range(len(text)):
#         os.system('adb shell input text ' + text[i])
        
def enterText_zhed(text, driver):
    """kwycode输入方法
    解决实名绑卡时，输入银行卡号乱序问题
    param:
        text:输入内容
        driver:服务实体，忽略
    """
    key = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
           'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
           'w', 'x', 'y', 'z']
    keyCode = ['7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '29', '30', '31',
               '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44',
               '45', '46', '47', '48', '49', '50', '51', '52', '53', '54']

    for s in str(text):
        for k in range(len(key)):
            if s == key[k]:
                driver.press_keycode(keyCode[k])
                time.sleep(1)
            else:
                pass


def phone_zhed():
    """
    手机认证
    """
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"1","vendor":"weshare","bizType":"zhed"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())

    headers = {'Content-Type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': '230221199009294217', 'authType': '1', 'vendor': 'weshare', 'bizType': 'zhed'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('手机认证成功')
        return flag
    else:
        flag = False
        print('手机认证失败')
        return flag


def XY_zhed():
    """
    信用卡认证
    """
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"12","vendor":"weshare","bizType":"zhed"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': '230221199009294217', 'authType': '12', 'vendor': 'weshare', 'bizType': 'zhed'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('信用卡认证成功')
        return flag
    else:
        flag = False
        print('信用卡认证失败')
        return flag

def taobao_zhed():
    """
    淘宝认证
    """
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"4","vendor":"weshare","bizType":"zhed"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': '230221199009294217', 'authType': '4', 'vendor': 'weshare', 'bizType': 'zhed'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('淘宝认证成功')
        return flag
    else:
        flag = False
        print('淘宝认证失败')
        return flag
        
def SF_zhed():
    """
    身份认证
    """
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"17","vendor":"weshare","bizType":"zhed"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': '230221199009294217', 'authType': '17', 'vendor': 'weshare', 'bizType': 'zhed'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('身份认证成功')
        return flag
    else:
        flag = False
        print('身份认证失败')
        return flag
        
def baseinfo_zhed():
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"45","vendor":"weshare","bizType":"zhed"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json','X-WeshareAuth-Token': token}
    data = {'idcard':'230221199009294217','authType':'45','vendor':'weshare','bizType':'zhed'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r= requests.post(url=url,data=json.dumps(data), headers=headers,verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('基本信息认证成功')
        return flag
    else:
        flag = False
        print('基本信息认证失败')
        return flag

def contact_zhed():
    """
    紧急联系人
    """
    flag = True
    expire_time = int(time.time()) + 186400
    sign = "|%s-%s-%d|%s|" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, '9qITW0TwF3y1CexxmhNbShBhCdsVcIQ3Uq1xZWXX')
    sign += '/mock/api/passAuthInfo' + '|POST||' + '{"idcard":"230221199009294217","authType":"13","vendor":"weshare","bizType":"zhed"}' + '|'
    m2 = hashlib.md5()
    m2.update(sign.encode('utf8'))
    token = "%s-%s-%s-%s" % ('v2', '46hsylbW6dChG0YOTQKN', expire_time, m2.hexdigest())
    headers = {'Content-Type': 'application/json', 'X-WeshareAuth-Token': token}
    data = {'idcard': '230221199009294217', 'authType': '13', 'vendor': 'weshare', 'bizType': 'zhed'}
    url = "https://sight1.zz.com/sight/mock/api/passAuthInfo"
    r = requests.post(url=url, data=json.dumps(data), headers=headers, verify=False)
    result = r.json()
    if result["respCode"] == "0000":
        print('紧急联系人认证成功')
        return flag
    else:
        flag = False
        print('紧急联系人认证失败')
        return flag


# 查询数据库是否有借款数据
def db_loan_daily_zhed():
    hosts = "20.1.1.11"
    ports = 4632
    users = "chenweinan"
    passwds = "nm3T47nkJZMTA7ar"
    dbs = "harbor_zhed"
    charsets = "utf8"
    t = int(time.time())

    conn = MySQLdb.connect(host=hosts, port=ports, user=users, passwd=passwds, db=dbs, charset=charsets)
    sql = 'select count(*) from record_loan where create_time > (%s - 10800) and loan_status = 6' %t
    cur = conn.cursor()
    cur.execute(sql)
    result = cur.fetchall()
    if result[0][0] > 0:
        print('数据库中有借款记录')
    else:
        print('数据库中暂无借款记录')




# 查询数据库是否有还款数据
def db_repayment_daily_zhed():
    hosts = "20.1.1.11"
    ports = 4632
    users = "chenweinan"
    passwds = "nm3T47nkJZMTA7ar"
    dbs = "harbor_zhed"
    charsets = "utf8"
    t = int(time.time())

    conn = MySQLdb.connect(host=hosts, port=ports, user=users, passwd=passwds, db=dbs, charset=charsets)
    sql = 'select count(*) from record_repayment where end_time > (%d - 10800) and repayment_status = 1' %t
    cur = conn.cursor()
    cur.execute(sql)
    result = cur.fetchall()

    if result[0][0] > 0:
        print('数据库中有还款记录')
    else:
        print('数据库中暂无还款记录')

"""
掌众财富
"""
def getCaptchaCode(type):
    """
    获取验证码图片、验证码json数据，请求验证码接口获得 验证码结果
    :param type:  xxw 学信网; renh 人行征信; job 简历-前程无忧  京东、百度、新浪微博 type传 "ot" licai 传财富验证码
    :return:
    """
    capurl = "https://sxapi.weshare.com.cn/moon/captcha/v1/recognize"
    captchaPATH = "/Users/weshare/Documents/weshare/anyproxy_new/android/caifu/captcha"
    if  type == "licai":
        with open(captchaPATH + "/licai", "rb") as f:
            content = f.read()
        captchaStream = base64.b64encode(content).decode('utf-8')
        print("captchaStream = %s" %captchaStream)
    else:
        print("验证码type输入有误！")

    # 获取图片验证码结果
    headers = {'X-WeshareAuth-Token': ''}

    data = {
        'captchaBody': captchaStream,
        'captchaType': '5000',
        'ignoreToken': True,
        'bizType': 'xiaomi',
        "channel": "default",
        "userGid": "user_gid",
        "idCardNo": "idcard_no",
        "userName": "userName",
        "phoneNo": "phoneNo",
        "channelSub": "channelSub",
        "ch": "ch",
        "systype": "systype"
    }
    print('url=%s,data=%s'%(capurl,json.dumps(data)))
    rr = requests.post(url=capurl, data=None, json=data, headers=headers,verify=False)
    res = json.loads(rr.text)
    print("response = %s" %res)
    vcode = res['vcode']
    print("pp:%s" % vcode)
    return vcode

def times():
    time.sleep(5)


def getSMSCode(type):
    """
    获取短信验证码
    :param type: 1淘宝、2.百度、3.手机运营商验证码,9.掌众财富
    :return:
    """
    time.sleep(30)
    url = 'http://20.1.1.210:5000/monkey/get_sms_info?sms_type=%s' %type
    r = requests.get(url=url)
    res = r.json()
    print(res)
    print(json.dumps(res,ensure_ascii=False))
    try:
        code = res['data']['sms_verify_code']
    except:
        print("未查找到验证码信息")
    try:
        if code.split():
            print("cc:%s" % code)
            return code
        else:
            print("获取短信验证码失败")
            raise Exception("获取短信验证码失败")
    except:
        print("获取验证码失败")




mobile = 17801779534
psw = "q123456789"
# header里的X-DEV-ID测试环境只校验了不为空
header = {"Content-Type": "application/json;charset=UTF-8",
      "X-DEV-ID": "ZWNlNWQ2YjExMzQ0NGZmNDgxMjkzNTBjN2Y2YThmMzF8OTU0MzU0MzMwNzc0NzY4"}
# unionId是唯一的，每次更换用户需要更换,存在user_base表中 ;u是user_base表的id
unionId = "opiq_jvcwu6OhN3rPYZG3KPO9CEY"
u = 767818

def MobileCheck():
    """
    获取codetoken
    """
    url = "https://capi.wealth365.com.cn/storm/userbase/mobilecheck/v1?c=2&b=8&ch=lca0yingyb"
    headers = header
    data = {
        "grantType": 1,
        "mobile": mobile
    }
    r = requests.post(url=url, data=json.dumps(data), headers=headers)
    codetoken = r.json()["content"]["codeToken"]
    print(codetoken)
    return codetoken


def Login():
    """
    理财用户登录
    """
    url = "https://capi.wealth365.com.cn/storm/userbase/userAppLogin/v1?c=2&b=8&ch=lca0yingyb"
    headers = header
    codetoken = MobileCheck()
    data = {
        "codeToken": codetoken,
        "grantType": 1,
        "mobile": mobile,
        "password": psw
    }

    r = requests.post(url=url, data=json.dumps(data), headers=headers)
    #print r.text
    usergid = r.json()["content"]["userGid"]
    usertoken = r.json()["content"]["userToken"]
    lglist = {"userGid": usergid, "userToken": usertoken}
    print(lglist)
    return lglist





def GetYuyueOrder():
    """
    获取用户预约中订单号
    """
    lglist = Login()
    usertoken = lglist["userToken"]
    usergid = lglist["userGid"]
    url = "https://capi.wealth365.com.cn/storm/lc/deposit/assets?c=2&token=%s&u=%s"%(usertoken,u)
    headers = header
    data = {
        "userGid": usergid,
        "c": 2,
        "chanel": "",
        "appver": "",
        "mt": "",
        "ct": ""
    }
    r = requests.post(url=url, data=json.dumps(data), headers=headers)
    productlist = r.json()["content"]["productList"]
    length = len(r.json()['content']['productList'])
    cancelorderlist = []

    #筛选出所有预约中未出借的订单
    for i in range(length):
        if productlist[i]["appointStatus"] == 0 and productlist[i]["loanCapital"] == 0 :
            cancelorderlist.append(productlist[i]["appointGid"])
    print ("订单：",cancelorderlist)
    return cancelorderlist

def cancelorder():
    """
    取消预约
    """
    orderlist = GetYuyueOrder()
    lglist = Login()
    usergid = lglist["userGid"]
    unionId = "opiq_jvcwu6OhN3rPYZG3KPO9CEY"
    header = {"Content-Type": "application/json;charset=UTF-8"}
    stoken = lglist["userToken"]
    u = 767818
    url = "https://capi.wealth365.com.cn/storm/trans/purchasecancel?c=2&token=%s&u=%s"%(stoken,u)
    length = len(orderlist)
    if length==0:
        print('无数据')
    else:
        for i in range(length):
            data = {
                "orderId": orderlist[i],
                "userGid": usergid,
                "unionId": unionId,
                "c": 2,
                "chanel": "",
                "appver": "",
                "mt": "",
                "ct": ""
                }
            r = requests.post(url=url, data=json.dumps(data), headers=header)
            print (r.text)
